package com.jxs.android.weiget;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.jxs.android.cmcc.R;

/**
 * @Author Jiangxs
 * @Date 2011-11-11 下午10:38:48
 * @Des 日期对胡框
 */
@SuppressLint("SimpleDateFormat")
public class WheelDateDialog extends Dialog implements Button.OnClickListener {

	private static final int START_YEAR = 1990, END_YEAR = 2100;

	private DateChangeListener dateChangeListener;

	private Button sureBtn;
	private Button cancelBtn;

	private int requestCode;

	private String currentTime;

	private WheelView wheelViewYear;
	private WheelView wheelViewMonth;
	private WheelView wheelViewDay;

	private String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
	private String[] months_little = { "4", "6", "9", "11" };

	public static java.text.SimpleDateFormat SDF = new java.text.SimpleDateFormat("yyyyMMdd");

	public WheelDateDialog(Context context, int requestCode, String currentTime) {
		super(context);
		this.requestCode = requestCode;
		this.currentTime = currentTime;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wheel_date);

		showDateTimePicker();
		sureBtn = (Button) findViewById(R.id.btn_datetime_sure);
		cancelBtn = (Button) findViewById(R.id.btn_datetime_cancel);

		sureBtn.setText("确定");
		cancelBtn.setText("取消");

		sureBtn.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);

	}

	private void showDateTimePicker() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);

		// 添加大小月月份并将其转换为list,方便之后的判断

		final List<String> list_big = Arrays.asList(months_big);
		final List<String> list_little = Arrays.asList(months_little);

		wheelViewYear = (WheelView) findViewById(R.id.year); // 年
		wheelViewYear.setAdapter(new NumericWheelAdapter(START_YEAR, END_YEAR));// 设置"年"的显示数据
		wheelViewYear.setCyclic(true);// 可循环滚动
		wheelViewYear.setLabel("年");// 添加文字
		wheelViewYear.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

		wheelViewMonth = (WheelView) findViewById(R.id.month); // 月
		wheelViewMonth.setAdapter(new NumericWheelAdapter(1, 12));
		wheelViewMonth.setCyclic(true);
		wheelViewMonth.setLabel("月");
		wheelViewMonth.setCurrentItem(month);

		wheelViewDay = (WheelView) findViewById(R.id.day); // 日
		wheelViewDay.setCyclic(true);

		if (list_big.contains(String.valueOf(month + 1))) {// 判断大小月及是否闰年,用来确定"日"的数据
			wheelViewDay.setAdapter(new NumericWheelAdapter(1, 31));
		} else if (list_little.contains(String.valueOf(month + 1))) {
			wheelViewDay.setAdapter(new NumericWheelAdapter(1, 30));
		} else {

			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { // 闰年
				wheelViewDay.setAdapter(new NumericWheelAdapter(1, 29));
			} else {
				wheelViewDay.setAdapter(new NumericWheelAdapter(1, 28));
			}
		}
		wheelViewDay.setLabel("日");
		wheelViewDay.setCurrentItem(day - 1);

		processCurrentTime(wheelViewYear, wheelViewMonth, wheelViewDay);

		wheelViewYear.addChangingListener(new OnWheelChangedListener() {// 滚动年监听器
					public void onChanged(WheelView wheel, int oldValue, int newValue) {
						int year_num = newValue + START_YEAR;
						if (list_big.contains(String.valueOf(wheelViewMonth.getCurrentItem() + 1))) { // 判断大小月及是否闰年,用来确定"日"的数据
							wheelViewDay.setAdapter(new NumericWheelAdapter(1, 31));
						} else if (list_little.contains(String.valueOf(wheelViewMonth.getCurrentItem() + 1))) {
							wheelViewDay.setAdapter(new NumericWheelAdapter(1, 30));
						} else {
							if ((year_num % 4 == 0 && year_num % 100 != 0) || year_num % 400 == 0) {
								wheelViewDay.setAdapter(new NumericWheelAdapter(1, 29));
							} else {
								wheelViewDay.setAdapter(new NumericWheelAdapter(1, 28));
							}
						}
					}
				});
		wheelViewMonth.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				if (list_big.contains(String.valueOf(month_num))) {// 判断大小月及是否闰年,用来确定"日"的数据
					wheelViewDay.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wheelViewDay.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if (((wheelViewYear.getCurrentItem() + START_YEAR) % 4 == 0 && (wheelViewYear.getCurrentItem() + START_YEAR) % 100 != 0) || (wheelViewYear.getCurrentItem() + START_YEAR) % 400 == 0) {
						wheelViewDay.setAdapter(new NumericWheelAdapter(1, 29));
					} else {
						wheelViewDay.setAdapter(new NumericWheelAdapter(1, 28));
					}
				}
			}
		});

		// 根据屏幕密度来指定选择器字体的大小
		int textSize = getContext().getResources().getDimensionPixelSize(R.dimen.BasicTextSize);
		wheelViewDay.TEXT_SIZE = textSize;
		wheelViewMonth.TEXT_SIZE = textSize;
		wheelViewYear.TEXT_SIZE = textSize;

	}

	private void processCurrentTime(WheelView wheelViewYear, WheelView wheelViewMonth, WheelView wheelViewDay) {
		if (null != currentTime && !"".equals(currentTime)) {
			try {
				java.util.Calendar currentDate = java.util.Calendar.getInstance();
				currentDate.setTime(SDF.parse(currentTime));
				if (null != currentDate) {
					wheelViewMonth.setCurrentItem(currentDate.get(Calendar.MONTH));
					wheelViewDay.setCurrentItem(currentDate.get(Calendar.DATE) - 1);
				}
			} catch (ParseException e) {
				Log.e("DateTimePicker", e.getMessage());
			}
		}
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == sureBtn.getId()) {
			if (null != dateChangeListener) {
				String parten = "00";// 如果是个数,则显示为"02"的样式
				DecimalFormat decimal = new DecimalFormat(parten); // 设置日期的显示
				String date = (wheelViewYear.getCurrentItem() + START_YEAR) + decimal.format((wheelViewMonth.getCurrentItem() + 1)) + decimal.format((wheelViewDay.getCurrentItem() + 1));
				dateChangeListener.onKeySave(requestCode, date);
			}
		}
		dismiss();

	}

	public void setDateChangeListener(DateChangeListener dateChangeListener) {
		this.dateChangeListener = dateChangeListener;
	}

	static public abstract interface DateChangeListener {
		public abstract void onKeySave(int requestCode, String dateTimeValue);
	}

}
